package org.zju.moviemotion;

import org.zju.moviemotion.DragGridView.OnStopDragListener;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class NumberPickerActivity extends Activity implements OnClickListener {

	private TextView textView;
	private DragGridView gridview;
	NumberAdapter adapter;
	private TextView tv_number1;
	private TextView tv_number2;
	private TextView tv_number3;
	private ImageButton forward;
	private ImageButton back;
	private Vibrator mVibrator;
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.numberpicker);
		DisplayMetrics dm = getResources().getDisplayMetrics();
		final int screenWidth = dm.widthPixels;
		final int screenHeight = dm.heightPixels - 50;
		
		mVibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
		
		tv_number1 = (TextView) findViewById(R.id.tv_number1);
		tv_number2 = (TextView) findViewById(R.id.tv_number2);
		tv_number3 = (TextView) findViewById(R.id.tv_number3);

		gridview = (DragGridView) findViewById(R.id.gridview);
		adapter = new NumberAdapter(this);
		gridview.setOnStopDragListener(new OnStopDragListener() {

			@Override
			public void onStop(float rawx, float rawy, int position) {
				/*if (isTouchPointInView(tv_number1, (int) rawx, (int) rawy)) {
					tv_number1.setBackgroundColor(Color.parseColor("#25B093"));
					int pos = position;
					if (pos == 9) {
						pos = 0;
					} else {
						pos = pos + 1;
					}
					tv_number1.setText(String.valueOf(pos));
					mVibrator.vibrate(80); // 震动一下
				} else*/ if (isTouchPointInView(tv_number2, (int) rawx,
						(int) rawy)) {
					tv_number2.setBackgroundColor(Color.parseColor("#25B093"));
					int pos = position;
					if (pos == 9) {
						pos = 0;
					} else {
						pos = pos + 1;
					}
					tv_number2.setText(String.valueOf(pos));
					mVibrator.vibrate(80); // 震动一下
				} else if (isTouchPointInView(tv_number3, (int) rawx,
						(int) rawy)) {
					tv_number3.setBackgroundColor(Color.parseColor("#25B093"));
					int pos = position;
					if (pos == 9) {
						pos = 0;
					} else {
						pos = pos + 1;
					}
					tv_number3.setText(String.valueOf(pos));
					mVibrator.vibrate(80); // 震动一下
				}
			}
		});
		gridview.setAdapter(adapter);

		forward = (ImageButton) findViewById(R.id.forward);
		forward.setOnClickListener(this);
		back = (ImageButton) findViewById(R.id.back);
		back.setOnClickListener(this);
	}

	private boolean isTouchPointInView(View view, int x, int y) {
		int[] location = new int[2];
		view.getLocationOnScreen(location);
		int left = location[0];
		int top = location[1];
		int right = left + view.getMeasuredWidth();
		int bottom = top + view.getMeasuredHeight();
		if (y >= top && y <= bottom && x >= left && x <= right) {
			return true;
		}
		return false;
	}

	@Override
	public void onClick(View v) {
		if (v == forward) {
			String age = tv_number1.getText().toString().trim()
					+ tv_number2.getText().toString().trim()
					+ tv_number3.getText().toString();
			MovieAplication.getInstance().setAge(Integer.parseInt(age));
			startActivity(new Intent(getBaseContext(),
					ColorPickerActivity.class));
		} else if (v == back) {
			finish();
		}
	}

}
